/*
 * @Author: Snow
 * @Date: 2021-09-15 17:18:10
 * @Description: 三维矩阵变换工具
 */
export class Matrix3 {
    public static create(): number[] {
        let out = [];
        out[0] = 1;
        out[1] = 0;
        out[2] = 0;
        out[3] = 0;
        out[4] = 1;
        out[5] = 0;
        out[6] = 0;
        out[7] = 0;
        out[8] = 1;
        return out;
    }

    public static identity(out: number[]): number[] {
        out[0] = 1;
        out[1] = 0;
        out[2] = 0;
        out[3] = 0;
        out[4] = 1;
        out[5] = 0;
        out[6] = 0;
        out[7] = 0;
        out[8] = 1;
        return out;
    }

    public static translation(tx: number, ty: number): number[] {
        return [
            1, 0, 0,
            0, 1, 0,
            tx, ty, 1,
        ];
    }

    public static rotation(angleInRadians: number): number[] {
        let c = Math.cos(angleInRadians);
        let s = Math.sin(angleInRadians);
        return [
            c, -s, 0,
            s, c, 0,
            0, 0, 1,
        ];
    }

    public static scaling(sx: number, sy: number): number[] {
        return [
            sx, 0, 0,
            0, sy, 0,
            0, 0, 1,
        ];
    }

    /**
     * Multiplies two mat3's
     *
     * @param {mat3} out the receiving matrix
     * @param {mat3} a the first operand
     * @param {mat3} b the second operand
     * @returns {mat3} out
     */
    public static multiply(a: number[], b: number[]): number[] {
        let out: number[] = [];
        let a00 = a[0];
        let a01 = a[1];
        let a02 = a[2];
        let a10 = a[3];
        let a11 = a[4];
        let a12 = a[5];
        let a20 = a[6];
        let a21 = a[7];
        let a22 = a[8];
        let b00 = b[0];
        let b01 = b[1];
        let b02 = b[2];
        let b10 = b[3];
        let b11 = b[4];
        let b12 = b[5];
        let b20 = b[6];
        let b21 = b[7];
        let b22 = b[8];
        out[0] = b00 * a00 + b01 * a10 + b02 * a20;
        out[1] = b00 * a01 + b01 * a11 + b02 * a21;
        out[2] = b00 * a02 + b01 * a12 + b02 * a22;
        out[3] = b10 * a00 + b11 * a10 + b12 * a20;
        out[4] = b10 * a01 + b11 * a11 + b12 * a21;
        out[5] = b10 * a02 + b11 * a12 + b12 * a22;
        out[6] = b20 * a00 + b21 * a10 + b22 * a20;
        out[7] = b20 * a01 + b21 * a11 + b22 * a21;
        out[8] = b20 * a02 + b21 * a12 + b22 * a22;
        return out;
    }

    /**
     * @Author: Snow
     * @description: 2D正交矩阵
     * @param {number} width
     * @param {number} height
     * @return {*}
     */
    public static projection(width: number, height: number): number[] {
        // 注意：这个矩阵翻转了 Y 轴，所以 0 在上方
        return [
            2 / width, 0, 0,
            0, -2 / height, 0,
            -1, 1, 1
        ];
    }


}
